-
-
Notifications
You must be signed in to change notification settings - Fork 11
feat: support global tolgee.t function call #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughThis update introduces support for extracting translation keys from calls to a global Changes
Sequence Diagram(s)sequenceDiagram
participant SourceCode
participant Parser
participant TokenMerger
participant Rule_globalTFunction
participant ExtractedKeys
SourceCode->>Parser: Parse source for translation calls
Parser->>TokenMerger: Apply mergers (including customTCallMerger)
TokenMerger-->>Parser: Return merged tokens
Parser->>Rule_globalTFunction: Trigger on 'tolgee.t' call
Rule_globalTFunction-->>Parser: Parse and extract key
Parser->>ExtractedKeys: Add extracted key
Poem
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
npm warn config production Use Note ⚡️ AI Code Reviews for VS Code, Cursor, WindsurfCodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback. Note ⚡️ Faster reviews with cachingCodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure ✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
src/extractor/parserVue/ParserVue.ts (1)
52-59: 💡 Verification agent🧩 Analysis chain
Verify that globalTFunction rule is properly integrated
The integration of
globalTFunctionas the first rule in the rules array seems correct. This enables the parser to extract translation keys from globaltolgee.tfunction calls.
🏁 Script executed:
#!/bin/bash # Verify that tolgee.t function calls are properly detected # Let's check the test files to ensure there are tests for this functionality echo "Checking for Vue tolgee.t tests:" fd ".*vue.*test.*" --type f | xargs grep -l "tolgee.t" | head -n 3Length of output: 141
Add test coverage for globalTFunction in Vue parser
I didn’t find any existing tests verifying that the
globalTFunctionrule correctly extractstolgee.t(…)calls in Vue single-file components. Please add or update your test suite to cover this new rule. For example:
- Create a test (e.g.
ParserVue.spec.tsorParserVue.test.ts) alongsidesrc/extractor/parserVue/ParserVue.ts.- Include a Vue SFC fixture using
tolgee.t('my.key')in both<template>and<script>.- Assert that running the parser yields the expected translation key.
🧹 Nitpick comments (2)
src/extractor/parser/rules/globalTFunction.ts (1)
5-10: New rule implementation for parsing global tolgee.t function callsThe implementation correctly defines a rule with the appropriate trigger and delegates to the general tFunction parser with context dependency set to false.
Consider reviewing the
as anytype assertion at the end of the declaration. While it might be necessary for compatibility reasons, type assertions toanycan sometimes hide potential type issues.-} satisfies RuleType<GeneralTokenType> as any; +} satisfies RuleType<GeneralTokenType>;If removing the assertion causes type errors, it might be worth documenting why it's needed.
src/extractor/parser/tokenMergers/customTCallMerger.ts (1)
12-14: Consider additional pattern matching flexibilityThe current implementation only matches expressions that start exactly with the accumulated state plus token. Consider adding more flexibility to handle variations like whitespace or alternative syntactic forms.
- if (customExpressions.find((e) => e.startsWith(state + token))) { + const nextState = state + token; + if (customExpressions.find((e) => e.startsWith(nextState) || + // Optional: More flexible matching logic here + e.replace(/\s+/g, '') === nextState.replace(/\s+/g, '') + )) { return state + token; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting
📒 Files selected for processing (16)
src/extractor/parser/generalMapper.ts(1 hunks)src/extractor/parser/parser.ts(2 hunks)src/extractor/parser/rules/globalTFunction.ts(1 hunks)src/extractor/parser/tokenMergers/customTCallMerger.ts(1 hunks)src/extractor/parser/tokenMergers/typesAsMerger.ts(1 hunks)src/extractor/parser/tokenMergers/typesCastMerger.ts(1 hunks)src/extractor/parserNgx/ParserNgx.ts(4 hunks)src/extractor/parserReact/ParserReact.ts(4 hunks)src/extractor/parserSvelte/ParserSvelte.ts(4 hunks)src/extractor/parserVue/ParserVue.ts(2 hunks)src/extractor/parserVue/rules/globalTFunction.ts(0 hunks)src/extractor/parserVue/tokenMergers/globalTFunctionMerger.ts(0 hunks)test/unit/extractor/ngx/translateService.test.ts(1 hunks)test/unit/extractor/react/useTranslate.test.ts(1 hunks)test/unit/extractor/svelte/getTranslate.test.ts(1 hunks)test/unit/extractor/vue/globalT.test.ts(2 hunks)
💤 Files with no reviewable changes (2)
- src/extractor/parserVue/rules/globalTFunction.ts
- src/extractor/parserVue/tokenMergers/globalTFunctionMerger.ts
🧰 Additional context used
🧬 Code Graph Analysis (6)
src/extractor/parserNgx/ParserNgx.ts (5)
src/extractor/parser/parser.ts (1)
DEFAULT_MERGERS(39-46)src/extractor/parser/rules/globalTFunction.ts (1)
globalTFunction(5-10)src/extractor/parserNgx/rules/componentWithT.ts (1)
componentWithT(13-52)src/extractor/parserNgx/rules/translatePipe.ts (1)
translatePipe(8-58)src/extractor/parserNgx/rules/translateFunction.ts (1)
translateFunction(5-10)
src/extractor/parser/rules/globalTFunction.ts (3)
src/extractor/parser/rules/tFunctionGeneral.ts (1)
tFunctionGeneral(7-52)src/extractor/parser/types.ts (1)
RuleType(65-68)src/extractor/parser/generalMapper.ts (1)
GeneralTokenType(102-117)
src/extractor/parserReact/ParserReact.ts (2)
src/extractor/parser/parser.ts (1)
DEFAULT_MERGERS(39-46)src/extractor/parser/rules/globalTFunction.ts (1)
globalTFunction(5-10)
src/extractor/parserVue/ParserVue.ts (3)
src/extractor/parser/parser.ts (1)
DEFAULT_MERGERS(39-46)src/extractor/parserVue/tokenMergers/hyphenPropsMerger.ts (1)
hyphenPropsMerger(11-30)src/extractor/parser/tokenMergers/customTCallMerger.ts (1)
customTCallMerger(4-25)
src/extractor/parser/tokenMergers/customTCallMerger.ts (2)
src/extractor/parser/mergerMachine.ts (1)
MachineType(9-23)src/extractor/parserReact/ParserReact.ts (1)
ReactMappedTokenType(21-23)
src/extractor/parserSvelte/ParserSvelte.ts (3)
src/extractor/parser/parser.ts (1)
DEFAULT_MERGERS(39-46)src/extractor/parser/rules/globalTFunction.ts (1)
globalTFunction(5-10)src/extractor/parserSvelte/rules/useTranslate.ts (1)
getTranslate(5-10)
⏰ Context from checks skipped due to timeout of 90000ms (3)
- GitHub Check: E2E Tests (ubuntu-latest, 22)
- GitHub Check: E2E Tests (ubuntu-latest, 20)
- GitHub Check: E2E Tests (ubuntu-latest, 18)
🔇 Additional comments (28)
src/extractor/parser/tokenMergers/typesAsMerger.ts (1)
7-7: Renamed merger for improved consistencyThe export name was corrected from
typesAsMergerertotypesAsMerger, removing the redundant "er" suffix and aligning with the consistent naming conventions used across parser token mergers.src/extractor/parser/tokenMergers/typesCastMerger.ts (1)
7-7: Renamed merger for improved consistencyThe export name was corrected from
typesCastMergerertotypesCastMerger, removing the redundant "er" suffix for consistent naming with other merger components.src/extractor/parser/generalMapper.ts (1)
116-117: Added new token type for global tolgee.t function supportAdded a new token type
'trigger.global.t.function'to theGeneralTokenTypetype alias, which enables the parser to recognize and extract keys from globaltolgee.tfunction calls.test/unit/extractor/react/useTranslate.test.ts (1)
540-551: Added test coverage for global tolgee.t function extractionNew test suite added to verify the extractor can correctly identify and extract translation keys from global
tolgee.tfunction calls in React components.The test ensures that the key extraction works properly with the new global function support, which is consistent with similar tests added for Vue, Svelte, and Angular frameworks.
src/extractor/parser/parser.ts (2)
24-26: Good naming consistency fix and addition of customTCallMergerThe renaming from
typesAsMergererandtypesCastMergerertotypesAsMergerandtypesCastMergerimproves naming consistency. Adding thecustomTCallMergerimport properly sets up support for globaltolgee.tfunction calls.
39-46: Well-structured changes to DEFAULT_MERGERSThe renaming from
DEFAULT_MERGERERStoDEFAULT_MERGERSmatches the improved naming convention, and includingcustomTCallMerger(['tolgee.t'])adds the necessary support for extracting translation keys from globaltolgee.tfunction calls.src/extractor/parserReact/ParserReact.ts (4)
2-2: Fixed import for DEFAULT_MERGERSCorrectly updated the import to use the renamed constant.
17-17: Good addition of globalTFunction importProperly imports the new parsing rule needed for global translation function support.
26-26: Fixed reference to DEFAULT_MERGERSUsing the renamed constant correctly.
45-51: Well-structured addition of globalTFunction ruleThe rules array is properly updated to include the new
globalTFunctionrule, positioned as the first rule to be evaluated. This ensures proper extraction of translation keys from globaltolgee.tfunction calls in React components.test/unit/extractor/vue/globalT.test.ts (2)
73-81: Improved test case clarityThe test description now more accurately reflects what's being tested: that only specific
$tusage patterns are extracted. Adding thefoo.$t('key3')call helps verify that translation calls on other objects are correctly ignored.
278-291: Good test coverage for global tolgee.t functionThis new test case effectively verifies that the extractor correctly identifies and extracts translation keys from global
tolgee.tfunction calls in Vue components. The test is simple but comprehensive, checking both key extraction and confirming no warnings are generated.test/unit/extractor/svelte/getTranslate.test.ts (1)
398-411: Good test coverage for global tolgee.t function in SvelteThis test case mirrors the Vue test, ensuring consistent behavior across frameworks. It verifies that the extractor correctly identifies and extracts translation keys from global
tolgee.tfunction calls in Svelte components.test/unit/extractor/ngx/translateService.test.ts (1)
161-172: Test looks good for basic global tolgee.t function supportThe test verifies that the extraction correctly identifies translation keys from global tolgee.t function calls. The implementation is straightforward and tests both the extraction of the key and the absence of warnings.
src/extractor/parserNgx/ParserNgx.ts (4)
2-2: Fixed typo in import statementCorrected the import from
DEFAULT_MERGERERStoDEFAULT_MERGERS.
16-16: Added import for globalTFunction ruleThis import is necessary to support the new functionality for extracting keys from global tolgee.t function calls.
25-25: Fixed typo in array spread operator usageUpdated to spread
DEFAULT_MERGERSinstead of the previously misspelledDEFAULT_MERGERERS.
43-43: Added globalTFunction rule to parser rulesThe rule is added as the first element in the array to ensure it takes precedence over other rules when applicable.
src/extractor/parserSvelte/ParserSvelte.ts (4)
2-2: Fixed typo in import statementCorrected the import from
DEFAULT_MERGERERStoDEFAULT_MERGERS.
18-18: Added import for globalTFunction ruleThis import is needed to support extraction of translation keys from global tolgee.t function calls in Svelte components.
27-27: Fixed typo in array spread operator usageUpdated to spread
DEFAULT_MERGERSinstead of the previously misspelledDEFAULT_MERGERERS.
46-46: Added globalTFunction rule to parser rulesThe rule is added as the first element in the array, consistent with how it was added in the other parsers. This ensures consistent behavior across frameworks.
src/extractor/parserVue/ParserVue.ts (4)
2-2: Fixed import name for consistencyThe import statement correctly uses
DEFAULT_MERGERSinstead of what was likelyDEFAULT_MERGERERS, ensuring consistency with the actual exported constant name from the parser module.
14-14: Good refactoring: Using shared globalTFunction ruleChanged from a local path to using the shared implementation from
../parser/rules/globalTFunction.js. This promotes code reuse and consistency across different parsers.
21-21: Improved architecture: Replaced Vue-specific merger with generic customTCallMergerThe addition of
customTCallMergerand its configuration with Vue-specific translation function names (['$t', 'this.$t']) replaces what was likely a Vue-specific implementation. This change creates a more unified approach to handling translation function calls across different framework parsers.Also applies to: 32-32
30-30: Fixed naming consistencyUpdated to use the correctly-named
DEFAULT_MERGERSconstant.src/extractor/parser/tokenMergers/customTCallMerger.ts (2)
4-25: Well-implemented generic token merger for translation function callsThe
customTCallMergeris a well-designed factory function that creates a token merger for custom translation function calls. It follows a state machine pattern to track and match token sequences, making it flexible for use across different parsers.The implementation:
- Takes an array of custom expressions to match
- Processes tokens based on their type ('variable', 'function.call', 'acessor.dot', 'expression.begin')
- Builds up state as it encounters tokens that match the provided expressions
- Triggers a merge when it encounters a complete match
- Marks merged tokens with the custom type 'trigger.global.t.function'
This design allows the same core logic to be reused across different parsers with different translation function naming patterns.
23-24: Good type safety with satisfies operatorThe use of
as const satisfies MachineType<ReactMappedTokenType, string>ensures type safety while maintaining a specific return type. This is a modern TypeScript best practice.
|
🎉 This PR is included in version 2.11.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Summary by CodeRabbit
New Features
tolgee.tfunction calls across React, Vue, Svelte, and Angular projects.Bug Fixes
Tests
tolgee.tfunction calls in React, Vue, Svelte, and Angular.Refactor